package com.supply.dl.utils.utils;

import android.content.Context;
import android.widget.ImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.supply.dl.R;
import com.supply.dl.utils.StringUtils;

import java.io.File;

public class ImgCacheUtil
{
	static final String TAG = "PicassoUtil";
	static Context context;
	static Picasso mPicasso;

	static final int SIZE_FACE_W = 128;
	static final int SIZE_FACE_H = 128;
	static final int SIZE_FACE_BIG_W = 256;
	static final int SIZE_FACE_BIG_H = 256;
	static final int SIZE_FACE_MINI_W = 128;
	static final int SIZE_FACE_MINI_H = 128;
	static final int SIZE_ALBUM_W = 128;
	static final int SIZE_ALBUM_H = 128;
	static final int SIZE_PREVIEW_W = 600;
	static final int SIZE_PREVIEW_H = 600;

	private static File getPicassoCaheDir(Context ctx)
	{
		//		return new File(Environment.getExternalStorageDirectory(), Constant.SD_PATH_IMAGE_CAHE_PICASSO);
		File f = new File(ctx.getExternalCacheDir(), "altar");
		return f;
	}

	public static void init(Context ctx)
	{
		context = ctx.getApplicationContext();
		mPicasso = getPicasso(ctx);
	}

	public static Picasso getPicasso()
	{
		return mPicasso;
	}

	public static Picasso getPicasso(Context context)
	{
		if (mPicasso == null)
		{
			synchronized (ImgCacheUtil.class)
			{
				if (mPicasso == null && context != null)
				{
					mPicasso = new Picasso.Builder(context).
							build();
				}
			}
		}
		return mPicasso;
	}

	public static boolean loadFromFileForAlbum(String thumb, String img, ImageView target)
	{
		File file = null;
		if (thumb != null && thumb.length() > 0)
		{
			file = new File(thumb);
			if (!file.exists())
				file = null;
		}
		if (file == null)
		{
			if (img != null && img.length() > 0)
				file = new File(img);
		}
		if (file == null)
			return false;
		getPicasso().load(file).resize(SIZE_ALBUM_W, SIZE_ALBUM_H).onlyScaleDown().centerCrop().into(target);
		return true;
	}

	public static void loadFromImageView(String url, ImageView img)
	{
		getPicasso().load(url).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher).into(img);
	}

	public static void loadFromFileForAlbum_cancel(ImageView target)
	{
		getPicasso().cancelRequest(target);
	}

	public static boolean loadFromFileForPreview(String img, ImageView target)
	{
		File file = null;
		if (file == null)
		{
			if (img != null && img.length() > 0)
				file = new File(img);
		}
		if (file == null)
			return false;
		getPicasso().load(file).resize(SIZE_PREVIEW_W, SIZE_PREVIEW_H).onlyScaleDown().centerInside().error(R.mipmap.ic_launcher).into(target);
		return true;
	}

	public static boolean loadFromUriForAvatar(String uri, int drawable, ImageView target)
	{
		if (StringUtils.isBlank(uri))
			return false;
		return loadFromUriForAvatar(uri, target, drawable, null);
	}

	public static boolean loadFromUriForAvatar(String uri, ImageView target, int drawableId, Callback callback)
	{
		if (drawableId == 0)
		{
			getPicasso().load(uri).resize(SIZE_FACE_W, SIZE_FACE_H).onlyScaleDown().centerInside().placeholder(drawableId).error(R.mipmap.ic_launcher).into(target, callback);
		}
		else if (drawableId == -100)
		{
			getPicasso().load(uri).resize(SIZE_FACE_W, SIZE_FACE_H).onlyScaleDown().centerInside().into(target, callback);
		}
		else
		{
			getPicasso().load(uri).resize(SIZE_FACE_W, SIZE_FACE_H).onlyScaleDown().centerInside().placeholder(drawableId).error(drawableId).into(target, callback);
		}
		return true;
	}

	public static boolean loadFromUriForAvatarBig(String uri, int drawableId, ImageView target)
	{
		if (drawableId == 0)
			getPicasso().load(uri).resize(SIZE_FACE_BIG_W, SIZE_FACE_BIG_H).onlyScaleDown().centerInside().error(R.mipmap.ic_launcher).into(target);
		else
			getPicasso().load(uri).resize(SIZE_FACE_BIG_W, SIZE_FACE_BIG_H).onlyScaleDown().centerInside().error(drawableId).into(target);

		return true;
	}

	public static boolean loadFromUriForAvatarMini(String uri, ImageView target)
	{
		getPicasso().load(uri).resize(SIZE_FACE_MINI_W, SIZE_FACE_MINI_H).onlyScaleDown().centerInside().error(R.mipmap.ic_launcher).into(target);
		return true;
	}

	public static void loadFromUriForAvatar(Context context, String face, ImageView ivHead)
	{
		getPicasso(context).cancelRequest(ivHead);
		if (face == null)
			ivHead.setImageResource(R.mipmap.ic_launcher);
		else
			getPicasso(context).load(face).placeholder(R.mipmap.ic_launcher).into(ivHead);
	}

	public static void loadFromUriForAvatarHD(Context context, String face, ImageView ivHead)
	{
		getPicasso(context).cancelRequest(ivHead);
		if (face == null)
			ivHead.setImageResource(R.mipmap.ic_launcher);
	}

	public static void loadFromUriForAlbum(Context context, String path, ImageView iv)
	{
		// TODO: 考虑缩放情况
		getPicasso(context).cancelRequest(iv);
		getPicasso(context).load(path).placeholder(R.mipmap.ic_launcher).into(iv);
	}

	public static boolean loadFromFileForCover(String path, ImageView target)
	{
		File file = null;
		if (file == null)
		{
			if (path != null && path.length() > 0)
				file = new File(path);
		}
		if (file == null)
			return false;
		getPicasso().load(file).fit().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(target);
		return true;
	}

	public static boolean loadFromUriForCover(String uri, ImageView target)
	{
		getPicasso().load(uri).fit().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(target);
		return true;
	}

	public static void loadBitMapByUri(String imgeUrl, ImageView imageView)
	{
		getPicasso().load(imgeUrl).placeholder(R.mipmap.ic_launcher).resize(SIZE_FACE_BIG_W, SIZE_FACE_BIG_H).centerCrop().into(imageView);
	}
}
